/**
 * 
 */
package com.anduinslegacy.bath;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.anduinslegacy.datastore.PMF;

/**
 * @author Stephen Jennings
 * 
 */
public class AuditFactory {
	public static void create(String _password, String _status, String _IP) {
		// Create a persistence manager object.
		PersistenceManager pm = getPM();

		// Create a new AuditEntry object.
		AuditEntry entry = new AuditEntry();

		// Create a Calendar object.
		Calendar calendar = Calendar.getInstance();

		// Create a Date that represents today.
		Date now = calendar.getTime();

		// Set the activity date
		entry.setActivity_date(now);
		
		// Set the attempted password
		entry.setPassword_used(_password);
		
		// Set the status (did this work or not?)
		entry.setStatus(_status);
		
		// Save the IP address
		entry.setIP_Address(_IP);
		
		// Save the changes.
		pm.makePersistent(entry);
	}

	@SuppressWarnings("unchecked")
	public static List<AuditEntry> get() {
		// Create a persistence manager object.
		PersistenceManager pm = getPM();
		Query query = pm.newQuery(AuditEntry.class);
		//query.setFilter("password_used == statusParam"); 
		//query.setOrdering("status"); 
		//query.declareParameters("String statusParam");		
		
		List<AuditEntry> entries = (List<AuditEntry>) query.execute();
		if (entries.isEmpty()) {
			return null;
		} else {
			return entries;
		}
	}
	
	private static PersistenceManager getPM() {
		PersistenceManager pm = PMF.get().getPersistenceManager();
		return pm;
	}
}
